package org.example.config;

import org.example.commons.cache.MyCacheEvictInterceptor;
import org.example.commons.cache.MyCacheableInterceptor;
import org.example.commons.cache.anno.MyCacheEvict;
import org.example.commons.cache.anno.MyCacheable;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class MyCacheAspect  {

    @Bean
    public PointcutAdvisor cacheablePointcutAdvisor() {
        MyCacheableInterceptor interceptor = new MyCacheableInterceptor();
        AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(null, MyCacheable.class);
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(pointcut);//切入点
        advisor.setAdvice(interceptor);//通知点
        //顾问 = @Aspect @Around(value = "annotation(org.example.commons.cache.anno.MyCacheable)")
        return advisor;
    }

    @Bean
    public DefaultPointcutAdvisor cacheEvictPointcutAdvisor() {
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(new AnnotationMatchingPointcut(null, MyCacheEvict.class, true));
        advisor.setAdvice(new MyCacheEvictInterceptor());
        return advisor;
    }

}
